package com.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.RoleBean;
import com.project.mapper.IGradeMapper;
import com.project.mapper.IRoleMapper;
import com.project.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleMapper roleMapper;

    @Autowired
    private IGradeMapper gradeMapper;
    @Override
    public List<RoleBean> findAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public void add(RoleBean role, Integer[] GradeIdArray) {

        roleMapper.insert(role);
        roleMapper.addCenter(role.getId(), GradeIdArray);
    }

    @Override
    public IPage<RoleBean> findByItem(Integer pageNO) {
        return roleMapper.selectPage(new Page(pageNO,3),null);
    }

    @Override
    public void del(Integer id) {
        roleMapper.delGrade(id);
        roleMapper.delUser(id);
        roleMapper.deleteById(id);
    }

    @Override
    public RoleBean findById(Integer id) {
        RoleBean role = roleMapper.selectById(id);
        role.setGradeBean(gradeMapper.findByRole(id));
        return role;
    }

    @Override
    public void update(Integer id, Integer[] gradeArray) {
        roleMapper.delGrade(id);
        roleMapper.addCenter(id,gradeArray);
    }
}
